package comp.style
{
	import comp.style.theme.*;
	
	import flash.events.EventDispatcher;
	import flash.system.ApplicationDomain;
	
	import mx.core.Singleton;
	
		
	dynamic public class StyleManager extends EventDispatcher implements IStyleManager
	{
		public function StyleManager()
		{
			this.setStyle("comp::Button",new ButtonTheme());
			this.setStyle("comp::CheckBox",new CheckBoxTheme());
			this.setStyle("comp::InputText",new InputTextTheme());
			this.setStyle("comp::Label",new LabelTheme());
			this.setStyle("comp::Panel",new PanelTheme());
			this.setStyle("comp::ScrollBar",new ScrollBarTheme());
			this.setStyle("comp::TextArea",new TextAreaTheme());
			
			this.setStyle("Global",new Global);
		}
		
		public function loadCss(css:Array=null):void
		{
			this.dispatchEvent(new StyleEvent(StyleEvent.STYLE_REGISTER_THEME));
			var item:*, itl:Class;
			
			for(var i:* in css)
			{
				item=css[i];
				if(item is IThemeLoaded)
					item.registerTheme();
			}
			
			this.dispatchEvent(new StyleEvent(StyleEvent.STYLE_CSS_LOADED));
		}
		
		public static function Initialized():void
		{
			Singleton.registerClass("comp.style.IStyleManager",comp.style.StyleManager);
		}

		private static var _inst:IStyleManager;
		
		public static function getInstance():IStyleManager
		{
			if(!_inst)
			{
				_inst=new StyleManager();
				Initialized();
			}
			return _inst;
		}
		
		/**
		 * 得到样式
		 */
		public function getStyle(styleProp:String):*
		{
			var v:*;

			if(_style)//样式表
			{
				if (styleProp in _style)
					return  _style[styleProp];
			}	
			
			//默认
			if (styleProp in this)
				return this[styleProp];
			
			if (v !== undefined) //
				return v;
			
			return undefined;
		}
		
		/**
		 * 设置样式
		 */
		public function setStyle(styleProp:String, newValue:*):void
		{
			if (!_style)
				_style = {};
			if(_style[styleProp])
				_style[styleProp]=undefined;
			_style[styleProp] = newValue;
			
		}

		//private
		private var _style:Object;
	}
}